import Vue from "vue";
import * as dayjs from "dayjs";

Vue.filter('NumberFormat', function (value, num) {
  if (!value) {
    return '0'
  }
  let intPartFormat = ''
  let arr = value.toString().split('.')
  if(num === undefined){
    num = arr.length > 1 ? arr[1].length : 0
  }
  intPartFormat = arr[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')//将整数部分逢三一断
  if(arr.length > 1){
    let fixednum = Number('0.'+arr[1]).toFixed(num)
    intPartFormat += '.' + fixednum.split('.')[1]
  }
  return intPartFormat
})

Vue.filter('dayjs', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

Vue.filter('moment', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

/** 字符串超长截取省略号显示 */
Vue.filter('ellipsis', function (value, vlength = 25) {
  if(!value){
    return "";
  }
  if (value.length > vlength) {
    return value.slice(0, vlength) + '...'
  }
  return value
})


/* Vue.filter('thousandNum',(num)=>{
  return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) { // 对整数部分添加分隔符
      return $1 + ",";
    });
  });
}) */

Vue.filter('hidephone',function(tel){
	var reg = /^(\d{3})\d{4}(\d{4})$/;  
	return tel.replace(reg, "$1****$2");
})
Vue.filter('hideEmail', function(email) {
  if (!email || email.indexOf('@') === -1) return email; // 非邮箱格式直接返回‌:ml-citation{ref="1" data="citationList"}
  return email.replace(/^(.{2}).*(@.+)$/, '$1****$2');
});